# Maintainer: Felix Yan <felixonmars@archlinux.org>

pkgname=rdma-core
pkgdesc='RDMA core userspace libraries and daemons'
pkgver=44.0
pkgrel=2
arch=('loong64' 'x86_64')
url="https://github.com/linux-rdma/rdma-core"
license=('GPL2' 'custom:OpenIB.org BSD (MIT variant)')
depends=('libnl' 'ethtool' 'perl' 'systemd-libs')
makedepends=('git' 'cmake' 'systemd' 'ninja' 'pandoc' 'libdrm' 'cython' 'python' 'python-docutils' 'valgrind')
_provides=('rdma' 'ibacm' 'libiwpm' 'libibcm' 'libibumad' 'libibverbs'
           'librdmacm' 'libcxgb3' 'libcxgb4' 'libmlx4' 'libmlx5' 'libmthca' 'libnes' 'libocrdma'
           'srptools' 'infiniband-diags' 'libibmad')
provides=("${_provides[@]}")
conflicts=("${_provides[@]}")
replaces=("${_provides[@]}")
backup=(etc/rdma/{rdma.conf,mlx4.conf})
source=("https://github.com/linux-rdma/rdma-core/archive/v$pkgver/$pkgname-$pkgver.tar.gz")
sha512sums=('7375bf352a28d8bec2525f518381596c39dec185fa06068a580dc31a386187351d039bbf28b6b1e3779d092ed492cf0ba882dbcd5c3c2290e3edf250e5545f19')
validpgpkeys=('42D25385C1A1C02B8B1B1C6F801BDDB825988F64') # nicolas@morey-chaisemartin.com

prepare() {
    mkdir -p build

    cd rdma-core-$pkgver
    find redhat -type f -exec sed --in-place \
        --expression='s|/usr/libexec|/usr/lib/rdma|g' \
        --expression='s|/usr/sbin|/usr/bin|g' \
        --expression='s|/sbin|/usr/bin|g' \
        '{}' '+'
}

build() {
    cd build
    cmake \
        -GNinja \
        -DCMAKE_INSTALL_PREFIX=/usr \
        -DCMAKE_INSTALL_RUNDIR=/run \
        -DCMAKE_INSTALL_SBINDIR=/usr/bin \
        -DCMAKE_INSTALL_LIBDIR=/usr/lib \
        -DCMAKE_INSTALL_LIBEXECDIR=/usr/lib/rdma \
        -DCMAKE_INSTALL_SYSCONFDIR=/etc \
        -DCMAKE_INSTALL_PERLDIR=/usr/share/perl5/vendor_perl \
        ../rdma-core-$pkgver
    ninja
}

check() {
    cd rdma-core-$pkgver
    PYTHONPATH="$srcdir/build/python" tests/run_tests.py
}

package() {
    cd build
    DESTDIR="$pkgdir" ninja install

    rm -vr "$pkgdir"/etc/init.d

    cd "$srcdir"/rdma-core-$pkgver/redhat
    install -D --mode=0644 rdma.conf "$pkgdir"/etc/rdma/rdma.conf
    install -D --mode=0755 rdma.mlx4-setup.sh "$pkgdir"/usr/lib/rdma/mlx4-setup.sh
    install -D --mode=0644 rdma.mlx4.conf "$pkgdir"/etc/rdma/mlx4.conf
    install -D --mode=0644 rdma.mlx4.sys.modprobe "$pkgdir"/usr/lib/modprobe.d/libmlx4.conf
    install -D --mode=0755 rdma.modules-setup.sh "$pkgdir"/usr/lib/dracut/modules.d/05rdma/module-setup.sh

    cd ..
    install -D --mode=0644 COPYING.BSD_MIT "$pkgdir"/usr/share/licenses/$pkgname/COPYING.BSD_MIT
}
